#!/bin/sh

total_steps=10

#$1,log file name
function check_file_exsit()
{
    if [ ! -f $1 ] ;
    then
        echo "file '$1',not exsit!" 
        exit
    fi
}

#$1,current step
#$2,total steps
#$3,description
function steps()
{
    printf "**********************step:%d/%d************************\n"  $1 $2
    if [ -n "$3" ] ;
    then
       printf "****step description:%s\n" "$3"
    fi
}

#$1,log file name;
function check_if_has_error()
{
    if grep -q -i error $1
    then
        #grep -n -E -i 'error' $1 | awk '{printf("\033[31m%s\033[0m\n",$0)}'
        grep -n -E -i 'error' $1 | awk '{printf("%s\n",$0)}'
        return 1
    else
        return 0
    fi
}

#$1,log file name;
function get_intrest()
{
    temp_file=`mktemp filt.log.XXXXXX`
    #grep -E -i -n 'error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg|setdata|getdata|sethead|gethead|setbody|getbody|cleanmsg|sendout|receivein|watch|CC-Request|Rating-Group =|+Requested-Service|CC-Total-Octets =|Session-Id =|CC-Input-Octets =|processid|RatingAgent::GetRouteId' $1 | grep -v 'ResetData'> /var/tmp/log/tmp.log

    search_str_1="error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg"           
    search_str="error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg|           \
        setdata|getdata|sethead|gethead|setbody|getbody|cleanmsg|       \
        sendout|receivein|watch|CC-Request|Rating-Group =|              \
        \+Requested-Service|CC-Total-Octets =|Session-Id =|             \
        CC-Input-Octets =|processid|Result-Code =|\+[A-Z]|-[A-Z]        \
        ::Execute()"
    search_str_component="::Execute\(\) (Begin|end)"

    unexpect_str="ResetData|ame[=_]|ALL|PAY"
    #echo $search_str
    grep -E -i -n "$search_str" $1 | grep -v -E "$unexpect_str" >$temp_file 
    #grep -E -i -n "$search_str_component|$search_str_1" $1 > $temp_file 
    IFS_OLD=$IFS
    IFS=$'\n'
    if [ -s $temp_file ] ; then
        index=0
        cat $temp_file | 
        while read -r line; do
            if [[ $line =~ "CleanMsg::Execute() Begin" ]] ;
            then
                ((index++))
                printf "\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>$index<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
            fi
            echo $line 
        done
    fi
    IFS=$IFS_OLD

    if [ -f $temp_file ];
    then
        rm $temp_file
    fi
}
#$1,log file name;
function get_intrest_component()
{
    temp_file=`mktemp filt.log.XXXXXX`
    #grep -E -i -n 'error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg|setdata|getdata|sethead|gethead|setbody|getbody|cleanmsg|sendout|receivein|watch|CC-Request|Rating-Group =|+Requested-Service|CC-Total-Octets =|Session-Id =|CC-Input-Octets =|processid|RatingAgent::GetRouteId' $1 | grep -v 'ResetData'> /var/tmp/log/tmp.log

    search_str_1="(error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg)"           
    search_str="error|RequestMsg|ReturnMsg|PayMsg|ConfirmMsg|           \
        setdata|getdata|sethead|gethead|setbody|getbody|cleanmsg|       \
        sendout|receivein|watch|CC-Request|Rating-Group =|              \
        \+Requested-Service|CC-Total-Octets =|Session-Id =|             \
        CC-Input-Octets =|processid|Result-Code =|\+[A-Z]|-[A-Z]        \
        ::Execute()"
    search_str_component="::Execute\(\) (Begin|end)"

    unexpect_str="ResetData|ame[=_]|ALL|PAY"
    #echo $search_str
    #grep -E -i -n "$search_str_component" $1 | grep -v -E "$unexpect_str" >$temp_file 
    grep -E -i -n "$search_str_component|$search_str_1" $1 > $temp_file 
    IFS_OLD=$IFS
    IFS=$'\n'
    if [ -s $temp_file ] ; then
        index=0
        cat $temp_file | 
        while read -r line; do
            if [[ $line =~ "CleanMsg::Execute() Begin" ]] ;
            then
                ((index++))
                printf "\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>$index<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
            fi
            if echo $line | grep -E -i "$search_str_1"
            then
                echo $line
            else
                line_num=`echo $line| awk -F ':' '{print $1}'`
                component_name=`echo $line | awk -F '::' '{print $1}'| awk '{print $NF}'`
                action=`echo $line | awk -F '::Execute' '{print $2}'| awk '{print $2}'`
                printf "%8dL    %-24s %s\n" $line_num $component_name $action
            fi

            #echo $line 
        done
    fi
    IFS=$IFS_OLD

    if [ -f $temp_file ];
    then
        rm $temp_file
    fi
}


################################main###################################
user_input_name=`basename $0`
if [ "$1" == "help" ] || [ "$1" == "h" ] || [ ! -n "$1" ];
then
    printf "%s  help|h                    see help information\n" "$user_input_name"
    printf "%s  log_file_name             see component and don not ignore error,exit when error.\n" "$user_input_name"
    printf "%s  log_file_name ne          see component and ignore error\n" "$user_input_name"
    printf "%s  log_file_name co          see component execute sequence,only print component.\n" "$user_input_name"
    exit;
fi
current_dir=`pwd`;
log_file_name=$1

check_file_exsit $log_file_name 

printf "current dir: %s\n" $current_dir
printf "log file name is: %s\n" `echo $log_file_name | awk -F '/' '{printf $NF}'`
printf "log file lines: %d\n" `wc -l $log_file_name | awk '{printf $1}'`

steps 1 $total_steps "check if it has error in the log."
check_if_has_error $log_file_name
if test 1 -eq $? ;
then
    printf "****error occur.\n\n"
    if [ "$2" != "ne" ] || [ "$2" == "component" ] || [ "$2" == "co" ];
    then
        exit;
    fi
else
    printf "****step 1 successed.\n\n"
fi


if [ "$2" == "component" ] || [ "$2" == "co" ];
then
    steps 2 $total_steps "check out intrest lines."
    get_intrest_component $log_file_name
    exit;
fi
steps 2 $total_steps "check out intrest lines."
get_intrest $log_file_name

printf "\n\n"
printf "current dir: %s\n" $current_dir
printf "log file name is: %s\n" $(echo $log_file_name | awk -F '/' '{printf $NF}')
printf "log file lines: %d\n" $(wc -l $log_file_name | awk '{printf $1}')


